<?php
namespace app\index\model;
use think\Db;
use think\Model;
class Friend extends Model
{

    

    

    // 表名
    protected $name = 'news_friend';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = "update_time";
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'age'
    ];
    public function members()
    {
        return $this->hasOne('Members','member_id','member_id')->bind(['mobile' => 'phone','netname'=>'nickname']);
    }
    public function getWechatNoAttr($value){
        if(strlen($value)>6){
            return substr_replace($value,'****',0,4);
        }else{
            return '****'.$value;
        }
    }
    public function getPhoneAttr($value){
        if(strlen($value)>7){
            return substr_replace($value,'****',3,4);
        }else{
            return '****'.$value;
        }
    }
    public function getSexNameAttr($value,$data)
    {
        switch ($value) {
            case 1:
                $name = "男";
                break;
            default:
                $name = "女";
                break;
        }
        return $name;
    }
    public function getEducationAttr($value,$data){
        switch($value){
            case '1':
                $education='初中及以下';
                break;
            case '2':
                $education= '中专/中技';
                break;
            case '3':
                $education= '高中';
                break;
            case '4':
                $education= '大专';
                break;
            case '5':
                $education= '本科';
                break;
            case '6':
                $education= '硕士';
                break;
            case '7':
                $education='博士';
                break;
            default:
                $education='不限';
                break;
        }
        return $education;

    }
    public function getAgeAttr($value,$data){
        if($data['birth']!=''){
            $arr=explode("-",$data['birth']);
            return date("Y")-$arr['0'];
        }else{
            return '';
        }
    }
    public function getAge1Attr($value,$data){

        $ey=date('Y');$em=date('m');$ed=date('d');
        $value = $value ? (is_numeric($value)?:strtotime($value)) : time();
        $ty=date('Y',$value);$tm=date('m',$value);$td=date('d',$value);

        $age=$ey-$ty;
        if($tm>$em||$tm==$em&&$td>$ed){
            $age--;
        }
        return $age;
    }

    public function getMtypeAttr($value, $data){
        switch ($value) {
            case 1:
                $name = "交友";
                break;
            case 2:
                $name = "结婚";
                break;
            case 3:
                $name = "旅游";
                break;
            default:
                $name = "其他";
                break;
        }
        return $name;
    }

    public function getMemberNameAttr($value,$data){
       $row = (new \app\index\model\Members)->get(['member_id'=>$value]);
       return !empty($row)?($row['nickname']??($row['username']??'火星人')):'火星人';
    }

    public function getThumburlAttr($value,$data){
        return $this->formatimg($value);
    }

    public function getCityTextAttr($value,$data)
    {
        return Db::name('area')->find($value)['name']??$value;
    }

    public function getAreaTextAttr($value,$data)
    {
        return Db::name('area')->find($value)['name']??$value;
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return $this->formattime($value);
    }

    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function member(){
        return $this->belongsTo('\app\admin\model\member\Account', 'member_id', 'member_id', [], 'LEFT')->setEagerlyType(0);
    }

    //转换图片
    private function formatimg($thumbs = [],$is_ad = 1){
      if(!empty($thumbs) && is_array($thumbs)) {
            foreach ($thumbs as $key => &$value) {
                if (isset($value['thumb']) && !empty($value['thumb'])) {
                    if (isset($value['type']) && $is_ad == 1) {
                        $value['ad_type'] = self::$ad_type[$value['type']];
                        $value['thumb'] = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $value['thumb'];
                    } else {
                        $value['thumb'] = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $value['thumb'];
                    }
                }
                if (isset($value['logo']) && !empty($value['logo'])) {
                    $value['logo'] = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $value['logo'];
                }
            }
        }elseif(!empty($thumbs) && is_string($thumbs)){
          //获取类型
          $url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $thumbs;
          $url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $thumbs;
          $thumbs = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:$thumbs);
        }
        return $thumbs;
    }

    //时间转换
    private function formattime($time){
        $str ='';
        $time = is_numeric($time)?$time: strtotime($time);
        $nowTime = time();
        if($time>$nowTime){
            return false;
        }

        $difftime = $nowTime - $time;
        if($difftime==0){
            $str = '刚刚';
        } elseif($difftime<60 && $difftime>0){
            $str = $difftime.'秒前';
        } elseif($difftime>= 60 && $difftime<= 60 * 60){
            $str = floor($difftime / 60).'分钟前';
        } elseif($difftime>= 60 && $difftime <= 3600 * 24){
            $str = floor($difftime / 3600).'小时前';
        } else if ($difftime > 3600 * 24 && $difftime <= 3600 * 24 * 7) {
            if (floor($difftime / (3600 * 24)) == 1) {
                $str = "昨天";
            } else if (floor($difftime / (3600 * 24)) == 2) {
                $str = "前天";
            } else {
                $str = floor($difftime / (3600 * 24)) . '天前';
            }
        }elseif($difftime > 3600 * 24 * 7){
            $str = date("Y-m-d", $time);
        }
        return $str;
    }

    public function getMemberNicknameAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['nickname']??'';
    }

    public function getMemberAvatarAttr($value,$data){
        $avatar = (new \app\index\model\Members)::get(['member_id'=>$value??0])['avatar']??'';
        if(!empty($avatar)){
             //获取类型
            $url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public' . $avatar;
            $url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public' . $avatar;
            $avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:$avatar);
        }else{
            $url1 = 'http://yijiahoutai.40b876.cn/yijia_admin/public/assets/img/avatar.png';
            $url2 = 'http://yijiahoutai.40b876.cn/yijia_store_admin/public/assets/img/avatar.png';
            $avatar = @getimagesize($url1) ? $url1 : (@getimagesize($url2)?$url2:'');
        }
       return $avatar;
    }
    
    public function getSignTextAttr($value,$data){
       return (new \app\index\model\Members)::get(['member_id'=>$value??0])['sign_text']??'';
    }

}
